package com.baosight.sczx.zp.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.cInterface.CmesConstants;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;

import com.baosight.sczx.zp.domain.ZPAuthzTree;


/**
 * 机构 修改
 * <p>Title: ServiceZP08.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 郭淑芳
 * @version 1.0
 * <pre>Histroy:
 *          2012-10-22 上午11:14:15  郭淑芳  Create
 *</pre>
 */
public class ServiceZP08 extends ServiceEPBase {

	private String name ="";
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP08</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-22上午11:14:35 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo)
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.zp.orgType"));//机构分类
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.zp.orgProperty",true));//机构性质
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.zp.business",true));//主管专业类型
		return outInfo;
	}
	
	/**
	 * 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP08</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-22上午11:14:53 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		 name = inInfo.getString("text");//树节点ID
		if(name != null){
			StaticTools.setQueryParameters(inInfo, "name", name, true);//添加查询条件
		    }
		EiInfo outInfo = super.query(inInfo, "ZPAuthzTree.query",new ZPAuthzTree());
		//outInfo.set("name", name);//部门id
		//outInfo.set("displayName", inInfo.getString("label"));//部门id
		return outInfo;
	}
	
	/**
	 * 修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP07</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-18上午06:25:26 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
       Map<String, String> params = new HashMap<String, String>();
		
       // 机构名称、机构编号是否重复
		boolean isRepeat = validate(inInfo, "ZPAuthzTree.validate", params);
		if(isRepeat) {
			return inInfo;
		}
		else {
			EiInfo outInfo = super.update(inInfo, "ZPAuthzTree.update");
			if(outInfo.getStatus() != -1) {
				CmesConstants.DEPARTMENT_STATUS = 1;
			}//更新缓存
			return outInfo;
		}
	}
	
	/**
	 * 校验机构编号、机构名称是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-22上午01:35:31 郭淑芳
	 * </p>
	 *
	 */
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String displayName = inInfo.getBlock("result").getCellStr(0, "displayName").toString();
		String ext3 = inInfo.getBlock("result").getCellStr(0, "ext3").toString();
		String name = inInfo.getBlock("result").getCellStr(0, "name").toString();
		
		params.put("displayName", displayName);		
		params.put("ext3", ext3);
		params.put("name", name);

		List result = dao.query(sqlName, params);
		if( result!=null && result.size() > 0 ) {
			inInfo.setStatus(-1);
			ZPAuthzTree authzTree = (ZPAuthzTree)result.get(0);
			StringBuffer msg = new StringBuffer("提交失败，");
			if ( authzTree.getDisplayName() != null && authzTree.getDisplayName().equals(displayName) ) {
				msg.append(" 机构名称 ");
			} else if( authzTree.getExt3() != null && authzTree.getExt3().equals(ext3) ) {
				msg.append(" 机构编号");
			}
			msg.append("重复！");
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}
}
